package com.zhida.smartcontrol.pagemy;

import com.alibaba.fastjson.JSON;
import com.jay.tools.utils.SharedUtil;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Keys;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.launch.login.bean.DepartsBean;
import com.zhida.smartcontrol.launch.login.bean.LoginBean;
import com.zhida.smartcontrol.launch.login.bean.UserInfoBean;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

import java.util.List;

/**
 * author: tanjianpei
 * date: 2019/10/8 008 17:03
 * description:
 */
public class PageMyPresenter extends BasePresenterImpl<PageMyContract.View> implements PageMyContract.Presenter {

    @Override
    public void fillInfo() {
        String json = SharedUtil.getString(Keys.USER_INFO, "");
        LoginBean bean = JSON.parseObject(json, LoginBean.class);
        UserInfoBean userInfo = bean.getUserInfo();
        List<DepartsBean> departs = bean.getDeparts();
        int multi_depart = bean.getMulti_depart();

        DepartsBean departBean = new DepartsBean();
        for (DepartsBean depart : departs) {
            if (depart.getOrgCode().equals(userInfo.getOrgCode())) {
                departBean = depart;
                break;
            }
        }

        mView.showInfo(userInfo, departBean);

    }

    @Override
    public void loginOut() {
        mKalleCenter.postJson(Api.LOGIN_OUT, null, true,new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                SharedUtil.remove(Keys.TOKEN);
                SharedUtil.remove(Keys.USER_INFO);

                mView.showMsg(msg);
                mView.loginOutOk();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
}
